package search.dao.Impl;

import commons.vo.LivegoodsResult;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Repository;
import search.dao.ArticleDao;
import search.pojo.Article;
import search.pojo.Esshipin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class ArticleDaoImpl implements ArticleDao {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Value("${livegoods.search.init.enabled}")
    private boolean initEnabled = false;

    @Override
    public LivegoodsResult queryForPage(String word, int page, int rows) {
//        // 高亮设置
//        HighlightBuilder.Field field = new HighlightBuilder.Field("title");
//        field.preTags("<span style='color:red'>");
//        field.postTags("</span>");

        BoolQueryBuilder shouldBuilder = QueryBuilders.boolQuery()
                .should(QueryBuilders.matchQuery("title",word))
                .should(QueryBuilders.matchQuery("content",word));

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        // 首先你这个type 这个字段属性设置的不太合理 ，正常你其实指定为 keyword就行
        // type 这样就就得 type.keyword 才是精准匹配
        queryBuilder
//                .must(QueryBuilders.matchQuery("type",type))
                .must(shouldBuilder);

        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withPageable(PageRequest.of(page,10))
                .build();

        // select *  from article  where type = xx and (title = xx or content = xx)
        // 是要查这个吧？
        long count = elasticsearchRestTemplate.count(query, Article.class);
        NativeSearchQuery query1 = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .build();
        long count1 = elasticsearchRestTemplate.count(query1, Article.class);
        if(count<=0) {
            ArrayList<Object> list = new ArrayList<>();
            LivegoodsResult ok = LivegoodsResult.ok(list);
            ok.setResults(count1);
        }

        SearchHits<Article> search = elasticsearchRestTemplate.search(query, Article.class);
        List<SearchHit<Article>> searchHits = search.getSearchHits();
        ArrayList<Article> list = new ArrayList<>();
        for (SearchHit<Article> searchHit : searchHits) {
//            // 处理高亮
//            Map<String, List<String>> highlightFieldMap = searchHit.getHighlightFields();

            Article article = new Article();
            article.setId(searchHit.getContent().getId());
            article.setTxt(searchHit.getContent().getTxt());
            article.setImg(searchHit.getContent().getImg());
            article.setTitle(searchHit.getContent().getTitle());
            article.setTag(searchHit.getContent().getTag());
            article.setLike(searchHit.getContent().getLike());
            article.setCollection(searchHit.getContent().getCollection());
            list.add(article);
        }
        LivegoodsResult ok = LivegoodsResult.ok(list);
        ok.setResults(count1);
        return ok;
    }

    @Override
    public LivegoodsResult EsPage(String word, int page) {

        BoolQueryBuilder shouldBuilder = QueryBuilders.boolQuery()
                .should(QueryBuilders.matchQuery("title",word));

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder
                .must(shouldBuilder);
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withPageable(PageRequest.of(page,12))
                .build();
        long count = elasticsearchRestTemplate.count(query, Esshipin.class);
        NativeSearchQuery query1 = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .build();
        long count1 = elasticsearchRestTemplate.count(query1, Esshipin.class);
        if(count<=0) {
            ArrayList<Object> list = new ArrayList<>();
            LivegoodsResult ok = LivegoodsResult.ok(list);
            ok.setResults(count1);
        }

        SearchHits<Esshipin> search = elasticsearchRestTemplate.search(query, Esshipin.class);
        List<SearchHit<Esshipin>> searchHits = search.getSearchHits();
        ArrayList<Esshipin> list = new ArrayList<>();
        for (SearchHit<Esshipin> searchHit : searchHits) {
            Esshipin article = new Esshipin();
            article.setId(searchHit.getContent().getId());
            article.setTxt(searchHit.getContent().getTxt());
            article.setImg(searchHit.getContent().getImg());
            article.setTitle(searchHit.getContent().getTitle());
            article.setTag(searchHit.getContent().getTag());
            article.setLike(searchHit.getContent().getLike());
            article.setCollection(searchHit.getContent().getCollection());
            list.add(article);
        }
        LivegoodsResult ok = LivegoodsResult.ok(searchHits);
        ok.setResults(count1);
        return ok;
    }


}
